<template>
  <div class="app-container">
    <RoomTree v-model="currentTree" @change="treeClick" />
    <div class="container_body">
      <el-row :gutter="10" class="mb8">
        <el-button type="primary" icon="el-icon-refresh-left" size="mini" @click="getList">刷新</el-button>
        <el-button
          type="success"
          icon="el-icon-help"
          size="mini"
          :disabled="multiple"
          @click="onSet"
          v-hasPermi="['param:tp:set']"
        >设置</el-button>
        <el-button
          type="warning"
          icon="el-icon-odometer"
          size="mini"
          :disabled="multiple"
          @click="zhaoce"
          v-hasPermi="['param:tp:call']"
        >召测</el-button>
      </el-row>

      <el-form
        :model="queryParams"
        ref="queryForm"
        size="small"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
      >
        <el-row>
          <el-form-item label="房间号码" prop="roomId">
            <RoomSelect v-model="queryParams.roomId" :params="roomSelectParams" />
          </el-form-item>
          <el-form-item label="通讯状态" prop="runStatus">
            <el-select v-model="queryParams.runStatus">
              <el-option label="全部" value=""></el-option>
              <el-option
                v-for="dict in dict.type.run_status"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="设备" prop="meterType">
            <el-select v-model="queryParams.meterType">
              <el-option label="全部" value=""></el-option>
              <el-option label="旧设备" value="0"></el-option>
              <el-option label="新设备" value="1"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-row>
      </el-form>

      <el-table
        v-loading="loading"
        :data="roomList"
        @selection-change="handleSelectionChange"
        ref="table"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="房间号码" align="center" prop="roomNo" />
        <el-table-column label="设备号" align="center" prop="meterNo" />
        <el-table-column label="通讯状态" align="center" prop="runStatus">
          <template slot-scope="scope">
            <dict-tag :value="scope.row.runStatus" :options="dict.type.run_status" />
          </template>
        </el-table-column>
        <el-table-column label="时段工作模式" align="center" prop="runType">
          <template slot-scope="scope">
            <dict-tag :value="scope.row.runType" :options="dict.type.time_work_mode" />
          </template>
        </el-table-column>
        <el-table-column label="周一时段" align="center" prop="edt1">
          <template slot-scope="scope">{{scope.row.sdt1}}-{{scope.row.edt1}}</template>
        </el-table-column>
        <el-table-column label="周二时段" align="center" prop="edt2">
          <template slot-scope="scope">{{scope.row.sdt2}}-{{scope.row.edt2}}</template>
        </el-table-column>
        <el-table-column label="周三时段" align="center" prop="edt3">
          <template slot-scope="scope">{{scope.row.sdt3}}-{{scope.row.edt3}}</template>
        </el-table-column>
        <el-table-column label="周四时段" align="center" prop="edt4">
          <template slot-scope="scope">{{scope.row.sdt4}}-{{scope.row.edt4}}</template>
        </el-table-column>
        <el-table-column label="周五时段" align="center" prop="edt5">
          <template slot-scope="scope">{{scope.row.sdt5}}-{{scope.row.edt5}}</template>
        </el-table-column>
        <el-table-column label="周六时段" align="center" prop="edt6" >
          <template slot-scope="scope">{{scope.row.sdt6}}-{{scope.row.edt6}}</template>
        </el-table-column>
        <el-table-column label="周日时段" align="center" prop="edt7">
          <template slot-scope="scope">{{scope.row.sdt7}}-{{scope.row.edt7}}</template>
        </el-table-column>
        <el-table-column label="更新时间" align="center" prop="updateDt" width="160">
          <template slot-scope="scope">{{ formatterDt(scope.row.updateDt) }}</template>
        </el-table-column>
        <el-table-column label="操作员" align="center" prop="userName" />
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>

    <ResultStatuListModal ref="ResultStatuListModal" @refresh="getList" />
    <Setting ref="setting" @refresh="getList" />
  </div>
</template>

<script>
import Setting from "./Setting.vue";
import ResultStatuListModal from "@/components/ResultStatuListModal";
import RoomSelect from "@/components/RoomSelect";
import {
  tpList,tpCall
} from "@/api/param/timeSpan";
import RoomTree from "@/components/RoomTree";
import FusionSelect from "@/components/FusionSelect";
import moment from 'moment';
export default {
  name: "Room",
  dicts: ["run_status", "time_work_mode"],
  components: {
    Setting,
    ResultStatuListModal,
    RoomTree,
    FusionSelect,
    RoomSelect,
    RoomSelect
  },
  data() {
    return {
      zhaoceList: [],
      // 当前选中层级
      currentTree: {},
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示查询条件
      showSearch: true,
      // 总条数
      total: 0,
      // 房间档案表格数据
      roomList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      roomSelectParams: {},
      getListParams: {},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      meterNos: [],
      selection: []
    };
  },
  created() {
    this.getList();
  },
  methods: {
    formatterDt(date) {
      return moment(date).format("yyyy-MM-DD HH:mm:ss");
    },
    /** 查询房间档案列表 */
    getList() {
      this.loading = true;
      tpList({
        ...this.queryParams,
        ...this.getListParams
      }).then(response => {
        this.roomList = response.rows.map(item => {
          item.meterType = Number(item.meterNo)> 1000 ? '0' : '1'
          return item
        });
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.selection = selection
      this.ids = selection.map(item => item.roomId);
      this.meterNos = selection.map(item => item.meterNo);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    zhaoce() {
      this.$refs.ResultStatuListModal.show({
        title: "您确定要召测数据？",
        actionName: '召测',
        total: this.selection.length,
        handler: (onItemOver) => tpCall({ rooms: this.selection, meterNos: this.meterNos, onItemOver })
      });
    },
    onSet() {
      this.$refs.setting.show(this.ids, this.selection);
    },
    treeClick() {
      const nameMap = {
        build: "buildId",
        floor: "floor",
        room: "roomId"
      };
      this.roomSelectParams = {
        [nameMap[this.currentTree.name]]: this.currentTree.id
      };
      const nameMap2 = {
        build: "buildId",
        floor: "floor",
        room: "roomId"
      };
      this.getListParams = {
        [nameMap2[this.currentTree.name]]: this.currentTree.id
          // this.currentTree.name == "floor"
          //   ? this.currentTree.data.name.substring(0, 1)
          //   : this.currentTree.id
      };
      this.handleQuery();
    }
  }
};
</script>

<style lang="scss" scoped>
.app-container {
  display: flex;
  overflow: hidden;
  .container_body {
    box-sizing: content-box;
    flex: 1;
    max-height: 100%;
    padding-left: 10px;
    overflow: auto;
    padding-right: 5px;
  }
}
</style>

